import axios from 'axios'
import { Toast } from 'vant'
import store from '@/store'
import JSONBig from 'json-bigint'

const request = axios.create({
  baseURL: 'http://toutiao-app.itheima.net',
  // baseURL: 'http://ttapi.research.itcast.cn/'
  transformResponse: [function (data) {
    // 对 data 进行任意转换处理
    try {
      return JSONBig.parse(data)
    } catch (e) {
      return data
    }
  }]
})
const notLoading = ['/v1_1/articles', '/v1_0/search', '/v1_0/comments']
request.interceptors.request.use(config => {
  if (store.state.user) {
    config.headers.Authorization = 'Bearer ' + store.state.user.token
  }
  if (notLoading.indexOf(config.url) === -1 && config.url.indexOf('/v1_0/articles/') === -1) {
    Toast.loading({
      duration: 0, // 持续展示 toast
      message: '加载中...',
      forbidClick: true
    })
  }
  return config
})

request.interceptors.response.use(res => {
  Toast.clear()
  return res.data.data
})
export default request
